package com.springcloud.market.api.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.springcloud.market.api.dto.UserDTO;
import com.springcloud.market.api.pojo.User;
import com.springcloud.market.api.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class Usercontroller {
    @Autowired
    private UserService userService;

    @RequestMapping("/addUser")
    public String addUser(){
        System.out.println("添加用户");
        return "添加用户";
    }

    @GetMapping("/getUser")
    @SentinelResource(value = "userApi", blockHandler = "handleDegradeBlock")
    public String getUser(){
        // 模拟业务逻辑：随机产生异常，模拟服务不稳定
        if (Math.random() > 0.7) {
            throw new RuntimeException("查询用户信息失败，数据库异常");
        }
        return userService.getUserInfo();
    }
    public String handleDegradeBlock(BlockException ex) {
        return "服务暂时不可用，请稍后重试! (触发熔断)";
    }

    @PostMapping("/registerUser")
    public User registerUser(@RequestBody UserDTO userDTO)
    {
        return userService.registerUser(userDTO);
    }
}
